//获取应用实例
const app = getApp()
var help = require('../../utils/help.js');
var login = require('../../utils/login.js');

Page({
  data: {
    imageUrl: app.data.imageUrl,
    data: {},
    status:0
  },
  onLoad: function () {
    
  },
  onShow: function () {
    this.loadData();
  },
  /**
   * 加载数据
   */
  loadData: function () {
    var that = this;
    var url = app.data.apiUrl + 'User/couponTypeList';
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
    };
    help.get(url, data, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        that.setData({
          data: result.data
        })
      } else {
        help.msg(result.msg)
        return false;
      }

    });
  },
  /**
   * 滚动到底部事件
   */
  onReachBottom: function () {
    
  },
  /**
   * 跳转详情页面
   */
  editAddress: function(e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    help.goUrl('/pages/user/addressAdd/addressAdd?id=' + id);
  },

  setStatus: function(e){
    this.setData({
      status: e.currentTarget.dataset.id
    })
    this.loadData();
  },

  lingqu: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要领取此优惠券吗？',
      success: function (res) {
        if (res.confirm) {
          var url = app.data.apiUrl + 'User/lingqu';
          var data = {
            token: app.data.userInfo ? app.data.userInfo.token : '',
            coupon_id: id,
          };
          help.get(url, data, that, function (res, that) {
            var result = res.data;
            console.log(result);
            if (result.code == 1) {
              help.msg(result.msg)
              that.loadData(true);
            } else {
              help.msg(result.msg)
              return false;
            }
          }, function (res, that) {
            wx.hideLoading()
            console.log(res);
            help.msg('网络异常！')
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
  
})
